package com.cspm.ahjy.bc.office.word.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.dom4j.Attribute;
import org.dom4j.Element;
import org.springframework.util.StringUtils;

import com.cspm.ahjy.bc.office.enums.ColorEnum;
import com.cspm.ahjy.bc.office.word.domain.MSAction;
import com.cspm.ahjy.bc.office.word.domain.MSActionType;
import com.cspm.ahjy.bc.office.word.domain.MSBody;
import com.cspm.ahjy.bc.office.word.domain.MSFont;
import com.cspm.ahjy.bc.office.word.domain.MSPicture;
import com.cspm.ahjy.bc.office.word.domain.MSWordart;

/**
 * 文字操作及匹配
 * 
 * @author ServerZhang
 * @date 2017年9月22日
 */
public class FontUtil {

	/**
	 * 添加文字
	 * 
	 * @param font2
	 * @return
	 * @author ServerZhang
	 * @date 2017年9月28日
	 */
	public static List<MSAction> addFont(MSFont font, Integer paragraphNo,
			Boolean wholeParagraph) {
		List<MSAction> actions = new ArrayList<MSAction>();

		List<MSAction> fontStyle = getFontStyle(font, paragraphNo,
				wholeParagraph);
		if (fontStyle != null && fontStyle.size() > 0) {
			actions.addAll(fontStyle);
		}

		return actions;
	}

	/**
	 * 段落中的普通文字比较
	 * 
	 * @param font1
	 * @param font2
	 * @param paragraphNo
	 *            比较的段落号
	 * @param wholeParagraph
	 *            是否是整段文字的比较，默认为false
	 * @return
	 * @author ServerZhang
	 * @date 2017年10月10日
	 */
	public static List<MSAction> compare(MSFont font1, MSFont font2,
			Integer paragraphNo, Boolean wholeParagraph) {
		if (wholeParagraph == null) {
			wholeParagraph = Boolean.FALSE;
		}

		List<MSAction> actions = new ArrayList<MSAction>();

		Integer[] paragraphNos = new Integer[1];
		if (paragraphNo != null) {
			paragraphNos[0] = paragraphNo;
		}

		String text1 = font1.getText();
		String text2 = font2.getText();

		String fontName1 = font1.getFontName();
		String fontName2 = font2.getFontName();
		if (fontName2 != null && !fontName2.equals(fontName1)) {
			MSAction action = new MSAction();
			MSActionType actionType = new MSActionType();
			if (wholeParagraph) {
				actionType.setType("paragraph_font_fontName");
				action.setText("");
			} else {
				actionType.setType("font_fontName");
				action.setText(text2);
			}
			actionType.setTypeValue(fontName2);
			action.setParagraphNos(paragraphNos);
			String content = "字体为 " + fontName2;
			actionType.setTypeContent(content);
			action.setMsActionType(actionType);
			actions.add(action);
		}

		Integer fontSize1 = font1.getFontSize();
		Integer fontSize2 = font2.getFontSize();
		if (!fontSize2.equals(fontSize1) && !fontSize2.equals(0)) {
			MSAction action = new MSAction();
			MSActionType actionType = new MSActionType();
			if (wholeParagraph) {
				actionType.setType("paragraph_font_fontSize");
				action.setText("");
			} else {
				actionType.setType("font_fontSize");
				action.setText(text2);
			}
			actionType.setTypeValue(fontSize2);
			action.setParagraphNos(paragraphNos);
			String content = "字号为 " + fontSize2;
			actionType.setTypeContent(content);
			action.setMsActionType(actionType);
			actions.add(action);
		}

		String color1 = font1.getColor();
		String color2 = font2.getColor();
		if (!color2.equals(color1)) {
			MSAction action = new MSAction();
			MSActionType actionType = new MSActionType();
			if (wholeParagraph) {
				actionType.setType("paragraph_font_color");
				action.setText("");
			} else {
				actionType.setType("font_color");
				action.setText(text2);
			}
			actionType.setTypeValue(color2);
			action.setParagraphNos(paragraphNos);
			
			String content = "颜色为“";
			ColorEnum byValue = ColorEnum.getByValue(color2);
			if (byValue != null) {
				content += byValue.getName();
			}else {
				content += color2;
			}
			content += "”";
			
			actionType.setTypeContent(content);
			action.setMsActionType(actionType);
			actions.add(action);
		}

		Boolean bold1 = font1.getBold();
		Boolean bold2 = font2.getBold();
		if (!bold2.equals(bold1)) {
			MSAction action = new MSAction();
			MSActionType actionType = new MSActionType();
			if (wholeParagraph) {
				actionType.setType("paragraph_font_bold");
				action.setText("");
			} else {
				actionType.setType("font_bold");
				action.setText(text2);
			}
			actionType.setTypeValue(bold2);
			action.setParagraphNos(paragraphNos);
			if (bold2) {
				actionType.setTypeContent("加粗");
			} else {
				actionType.setTypeContent("取消加粗");
			}
			action.setMsActionType(actionType);
			actions.add(action);
		}

		Boolean italic1 = font1.getItalic();
		Boolean italic2 = font2.getItalic();
		if (!italic2.equals(italic1)) {
			MSAction action = new MSAction();
			MSActionType actionType = new MSActionType();
			if (wholeParagraph) {
				actionType.setType("paragraph_font_italic");
				action.setText("");
			} else {
				actionType.setType("font_italic");
				action.setText(text2);
			}
			actionType.setTypeValue(italic2);
			action.setParagraphNos(paragraphNos);
			if (italic2) {
				actionType.setTypeContent("倾斜");
			} else {
				actionType.setTypeContent("取消倾斜");
			}
			action.setMsActionType(actionType);
			actions.add(action);
		}

		String underline1 = font1.getUnderline();
		String underline2 = font2.getUnderline();
		if (!underline2.equals(underline1)) {
			MSAction action = new MSAction();
			MSActionType actionType = new MSActionType();
			if (wholeParagraph) {
				actionType.setType("paragraph_font_underline");
				action.setText("");
			} else {
				actionType.setType("font_underline");
				action.setText(text2);
			}
			actionType.setTypeValue(underline2);
			action.setParagraphNos(paragraphNos);
			if ("".equals(underline2)) {
				actionType.setTypeContent("取消下划线");
			} else {
				actionType.setTypeContent("添加下划线，样式为：" + underline2);
			}
			action.setMsActionType(actionType);
			actions.add(action);
		}

		Boolean strike1 = font1.getStrike();
		Boolean strike2 = font2.getStrike();
		if (!strike2.equals(strike1)) {
			MSAction action = new MSAction();
			MSActionType actionType = new MSActionType();
			if (wholeParagraph) {
				actionType.setType("paragraph_font_strike");
				action.setText("");
			} else {
				actionType.setType("font_strike");
				action.setText(text2);
			}
			actionType.setTypeValue(strike2);
			action.setParagraphNos(paragraphNos);
			if (strike2) {
				actionType.setTypeContent("添加删除线");
			} else {
				actionType.setTypeContent("取消删除线");
			}
			action.setMsActionType(actionType);
			actions.add(action);
		}

		Boolean dbStrike1 = font1.getDbStrike();
		Boolean dbStrike2 = font2.getDbStrike();
		if (!dbStrike2.equals(dbStrike1)) {
			MSAction action = new MSAction();
			MSActionType actionType = new MSActionType();
			if (wholeParagraph) {
				actionType.setType("paragraph_font_dbStrike");
				action.setText("");
			} else {
				actionType.setType("font_dbStrike");
				action.setText(text2);
			}
			actionType.setTypeValue(dbStrike2);
			action.setParagraphNos(paragraphNos);
			if (dbStrike2) {
				actionType.setTypeContent("添加双删除线");
			} else {
				actionType.setTypeContent("取消双删除线");
			}
			action.setMsActionType(actionType);
			actions.add(action);
		}

		String stress1 = font1.getStress();
		String stress2 = font2.getStress();
		if (!stress2.equals(stress1)) {
			MSAction action = new MSAction();
			MSActionType actionType = new MSActionType();
			if (wholeParagraph) {
				actionType.setType("paragraph_font_stress");
				action.setText("");
			} else {
				actionType.setType("font_stress");
				action.setText(text2);
			}
			actionType.setTypeValue(stress2);
			action.setParagraphNos(paragraphNos);
			if ("".equals(stress2)) {
				actionType.setTypeContent("取消着重号");
			} else {
				actionType.setTypeContent("添加着重号，样式为：" + stress2);
			}
			action.setMsActionType(actionType);
			actions.add(action);
		}

		return actions;
	}

	/**
	 * 
	 * @param answer
	 *            标准答案文档
	 * @param task
	 *            学生提交文档
	 * @param paragraphNums
	 *            匹配的段落
	 * @param text
	 *            匹配的文字
	 * @param settings
	 *            对应的文字操作
	 * @return
	 * @author ServerZhang
	 * @date 2017年9月22日
	 */
	public static Map<String, Boolean> mark(MSBody answer, MSBody work,
			List<Integer> paragraphNums, String text,
			Map<String, Object> settings) {
		Map<String, Boolean> map = new HashMap<String, Boolean>();

		if (null == paragraphNums || paragraphNums.size() <= 0
				|| null == settings || settings.size() <= 0) {
			return map;
		}

		List<MSFont> fonts1 = BodyUtil.getFonts(answer, paragraphNums, text);
		List<MSFont> fonts2 = BodyUtil.getFonts(work, paragraphNums, text);

		if (fonts2.size() <= 0) {
			return map;
		}

		Set<String> keySet = settings.keySet();
		for (String key : keySet) {
			Object object = settings.get(key);
			if (null == object) {
				continue;
			}
			String objStr = object.toString();
			map.put(key, true);// 默认此项操作正确
			if (key.equals("font")) {
				Iterator<MSFont> iterator = fonts2.iterator();
				while (iterator.hasNext()) {
					MSFont next = iterator.next();
					if (!objStr.equals(next.getFontName())) {// 只要有一处不匹配视为此项操作不正确
						map.put(key, false);
						System.out.println("未将指定文字“" + text + "”设置字体为："
								+ objStr);
						break;
					}
				}
			} else if (key.equals("size")) {
				Iterator<MSFont> iterator = fonts2.iterator();
				while (iterator.hasNext()) {
					MSFont next = iterator.next();
					Integer parseInt = Integer.parseInt(objStr);
					if (!parseInt.equals(next.getFontSize())) {// 只要有一处不匹配视为此项操作不正确
						map.put(key, false);
						System.out.println("未将指定文字“" + text + "”设置大小为："
								+ objStr);
						break;
					}
				}
			} else if (key.equals("color")) {
				Iterator<MSFont> iterator = fonts2.iterator();
				while (iterator.hasNext()) {
					MSFont next = iterator.next();
					if (!objStr.equals(next.getColor())) {// 只要有一处不匹配视为此项操作不正确
						map.put(key, false);
						System.out.println("未将指定文字“" + text + "”设置颜色为："
								+ objStr);
						break;
					}
				}
			} else if (key.equals("bold")) {
				Iterator<MSFont> iterator = fonts2.iterator();
				while (iterator.hasNext()) {
					MSFont next = iterator.next();
					Boolean parseBoolean = Boolean.parseBoolean(objStr);
					if (!parseBoolean.equals(next.getBold())) {// 只要有一处不匹配视为此项操作不正确
						map.put(key, false);
						if (parseBoolean) {
							System.out.println("未将指定文字“" + text + "”加粗");
						} else {
							System.out.println("未将指定文字“" + text + "”取消加粗");
						}
						break;
					}
				}
			} else if (key.equals("italic")) {
				Iterator<MSFont> iterator = fonts2.iterator();
				while (iterator.hasNext()) {
					MSFont next = iterator.next();
					Boolean parseBoolean = Boolean.parseBoolean(objStr);
					if (!parseBoolean.equals(next.getItalic())) {// 只要有一处不匹配视为此项操作不正确
						map.put(key, false);
						if (parseBoolean) {
							System.out.println("未将指定文字“" + text + "”设置为斜体");
						} else {
							System.out.println("未将指定文字“" + text + "”取消斜体");
						}
						break;
					}
				}
			} else if (key.equals("underline")) {
				Iterator<MSFont> iterator = fonts2.iterator();
				while (iterator.hasNext()) {
					MSFont next = iterator.next();
					if (!objStr.equals(next.getUnderline())) {// 只要有一处不匹配视为此项操作不正确
						map.put(key, false);
						System.out.println("未将指定文字“" + text + "”按要求设置下划线样式");
						break;
					}
				}
			} else if (key.equals("underlineColor")) {
				Iterator<MSFont> iterator = fonts2.iterator();
				while (iterator.hasNext()) {
					MSFont next = iterator.next();
					if (!objStr.equals(next.getUnderlineColor())) {// 只要有一处不匹配视为此项操作不正确
						map.put(key, false);
						System.out.println("未将指定文字“" + text + "”按要求设置下划线颜色");
						break;
					}
				}
			}
		}

		return map;

	}

	/**
	 * 解析<w:r>标签
	 * 
	 * @param rElement
	 * @param pCount
	 *            段落号
	 * @param path 文档所在目录
	 * @return
	 * @author ServerZhang
	 * @date 2017年9月28日
	 */
	public static MSFont getFont(Element rElement, Integer pCount, String path) {
		MSFont msFont = new MSFont();
		msFont.setParagraphNum(pCount);
		Element rpr = rElement.element("rPr");// 获取文字样式

		if (rpr != null) {
			// 获取文字样式
			Element fontE = rpr.element("font");
			if (null != fontE) {
				Attribute attribute = fontE.attribute("val");
				if (attribute != null) {
					msFont.setFontName(attribute.getText());
				}
			}
			Element szE = rpr.element("sz");
			if (null != szE) {
				Attribute attribute = szE.attribute("val");
				if (attribute != null) {
					msFont.setFontSize(Integer.parseInt(attribute.getText()) / 2);
				}
			}
			Element colorE = rpr.element("color");
			if (null != colorE) {
				Attribute attribute = colorE.attribute("val");
				if (attribute != null) {
					msFont.setColor(attribute.getText());
				}
			}
			Element uE = rpr.element("u");
			if (null != uE) {
				Attribute attribute = uE.attribute("val");
				if (attribute != null) {
					msFont.setUnderline(attribute.getText());
				}
				Attribute attribute2 = uE.attribute("color");
				if (attribute2 != null) {
					msFont.setUnderlineColor(attribute2.getText());
				}
			}
			Element emE = rpr.element("em");
			if (null != emE) {
				Attribute attribute = emE.attribute("val");
				if (attribute != null) {
					msFont.setStress(attribute.getText());
				}
			}
			Element bE = rpr.element("b");
			if (bE != null) {
				msFont.setBold(true);
			}
			Element iE = rpr.element("i");
			if (iE != null) {
				msFont.setItalic(true);
			}
			Element strikeE = rpr.element("strike");
			if (strikeE != null) {
				msFont.setStrike(true);
			}
			Element dstrikeE = rpr.element("dstrike");
			if (dstrikeE != null) {
				msFont.setDbStrike(true);
			}
		}

		Element t = rElement.element("t");// 获取文字内容
		if (t != null) {
			String text = t.getText();
			msFont.setText(text);
			// System.out.println(text+"--"+pCount);
			return msFont;
		}

		Element pict = rElement.element("pict");// 获取图片或艺术字内容
		if (null == pict) {
			return msFont;
		}

		Element shape = pict.element("shape");//
		if (null == shape) {
			return msFont;
		}
		Element imgElement = shape.element("imagedata");
		if (null == imgElement) {// 艺术字
			System.out.println("这里是艺术字--" + pCount);
			MSWordart wordart = WordartUtil.getWordart4Pict(pict);

			msFont.setType(1);// 类型设置，艺术字
			msFont.setText(wordart.getText());
			msFont.setMsWordart(wordart);
		} else {// 图片
			System.out.println("这里是图片--" + pCount);
			MSPicture picture = PictureUtil.getPicture4Pict(pict,path);

			msFont.setType(2);// 类型设置，图片
			msFont.setMsPicture(picture);
		}

		return msFont;
	}

	/**
	 * 获取字体的所有样式
	 * 
	 * @param font
	 * @author ServerZhang
	 * @date 2017年9月29日
	 */
	private static List<MSAction> getFontStyle(MSFont font,
			Integer paragraphNo, Boolean wholeParagraph) {
		List<MSAction> actions = new ArrayList<MSAction>();

		Integer[] paragraphNos = new Integer[1];
		if (paragraphNo != null) {
			paragraphNos[0] = paragraphNo;
		}

		String text = font.getText();

		String fontName = font.getFontName();

		Integer fontSize = font.getFontSize();

		String color = font.getColor();

		Boolean bold = font.getBold();

		Boolean italic = font.getItalic();

		String underline = font.getUnderline();
		String underlineColor = font.getUnderlineColor();

		Boolean strike = font.getStrike();

		Boolean dbStrike = font.getDbStrike();

		String stress = font.getStress();

		if (!StringUtils.isEmpty(fontName)) {
			MSAction action = new MSAction();
			MSActionType actionType = new MSActionType();
			if (wholeParagraph) {
				actionType.setType("paragraph_font_fontName");
				action.setText("");
			} else {
				actionType.setType("font_fontName");
				action.setText(text);
			}
			actionType.setTypeValue(fontName);
			action.setParagraphNos(paragraphNos);
			String content = "字体为 " + fontName;
			actionType.setTypeContent(content);
			action.setMsActionType(actionType);
			actions.add(action);
		}
		if (!StringUtils.isEmpty(fontSize) && !fontSize.equals(0)) {
			MSAction action = new MSAction();
			MSActionType actionType = new MSActionType();
			if (wholeParagraph) {
				actionType.setType("paragraph_font_fontSize");
				action.setText("");
			} else {
				actionType.setType("font_fontSize");
				action.setText(text);
			}
			actionType.setTypeValue(fontSize);
			action.setParagraphNos(paragraphNos);
			String content = "字号为 " + fontSize;
			actionType.setTypeContent(content);
			action.setMsActionType(actionType);
			actions.add(action);
		}
		if (!StringUtils.isEmpty(color) && !color.equals("000000")) {
			MSAction action = new MSAction();
			MSActionType actionType = new MSActionType();
			if (wholeParagraph) {
				actionType.setType("paragraph_font_color");
				action.setText("");
			} else {
				actionType.setType("font_color");
				action.setText(text);
			}
			actionType.setTypeValue(color);
			action.setParagraphNos(paragraphNos);
			
			String content = "颜色为“";
			ColorEnum byValue = ColorEnum.getByValue(color);
			if (byValue != null) {
				content += byValue.getName();
			}else {
				content += color;
			}
			content += "”";
			
			actionType.setTypeContent(content);
			action.setMsActionType(actionType);
			actions.add(action);
		}
		if (bold) {
			MSAction action = new MSAction();
			MSActionType actionType = new MSActionType();
			if (wholeParagraph) {
				actionType.setType("paragraph_font_bold");
				action.setText("");
			} else {
				actionType.setType("font_bold");
				action.setText(text);
			}
			actionType.setTypeValue(bold);
			action.setParagraphNos(paragraphNos);
			actionType.setTypeContent("加粗");
			action.setMsActionType(actionType);
			actions.add(action);
		}
		if (italic) {
			MSAction action = new MSAction();
			MSActionType actionType = new MSActionType();
			if (wholeParagraph) {
				actionType.setType("paragraph_font_italic");
				action.setText("");
			} else {
				actionType.setType("font_italic");
				action.setText(text);
			}
			actionType.setTypeValue(italic);
			action.setParagraphNos(paragraphNos);
			actionType.setTypeContent("倾斜");
			action.setMsActionType(actionType);
			actions.add(action);
		}
		if (!StringUtils.isEmpty(underline)) {
			MSAction action = new MSAction();
			MSActionType actionType = new MSActionType();
			if (wholeParagraph) {
				actionType.setType("paragraph_font_underline");
				action.setText("");
			} else {
				actionType.setType("font_underline");
				action.setText(text);
			}
			actionType.setTypeValue(underline);
			action.setParagraphNos(paragraphNos);
			actionType.setTypeContent("添加下划线，样式为：" + underline);
			action.setMsActionType(actionType);
			actions.add(action);
		}
		if (!StringUtils.isEmpty(underlineColor)) {
			MSAction action = new MSAction();
			MSActionType actionType = new MSActionType();
			if (wholeParagraph) {
				actionType.setType("paragraph_font_underline_color");
				action.setText("");
			} else {
				actionType.setType("font_underline_color");
				action.setText(text);
			}
			actionType.setTypeValue(underlineColor);
			action.setParagraphNos(paragraphNos);
			
			String content = "添加下划线，颜色为“";
			ColorEnum byValue = ColorEnum.getByValue(underlineColor);
			if (byValue != null) {
				content += byValue.getName();
			}else {
				content += underlineColor;
			}
			content += "”";
			
			actionType.setTypeContent(content);
			action.setMsActionType(actionType);
			actions.add(action);
		}
		if (strike) {
			MSAction action = new MSAction();
			MSActionType actionType = new MSActionType();
			if (wholeParagraph) {
				actionType.setType("paragraph_font_strike");
				action.setText("");
			} else {
				actionType.setType("font_strike");
				action.setText(text);
			}
			actionType.setTypeValue(strike);
			action.setParagraphNos(paragraphNos);
			actionType.setTypeContent("添加删除线");
			action.setMsActionType(actionType);
			actions.add(action);
		}
		if (dbStrike) {
			MSAction action = new MSAction();
			MSActionType actionType = new MSActionType();
			if (wholeParagraph) {
				actionType.setType("paragraph_font_dbStrike");
				action.setText("");
			} else {
				actionType.setType("font_dbStrike");
				action.setText(text);
			}
			actionType.setTypeValue(dbStrike);
			action.setParagraphNos(paragraphNos);
			actionType.setTypeContent("添加双删除线");
			action.setMsActionType(actionType);
			actions.add(action);
		}
		if (!StringUtils.isEmpty(stress)) {
			MSAction action = new MSAction();
			MSActionType actionType = new MSActionType();
			if (wholeParagraph) {
				actionType.setType("paragraph_font_stress");
				action.setText("");
			} else {
				actionType.setType("font_stress");
				action.setText(text);
			}
			actionType.setTypeValue(stress);
			action.setParagraphNos(paragraphNos);
			actionType.setTypeContent("添加着重号，样式为：" + stress);
			action.setMsActionType(actionType);
			actions.add(action);
		}
		return actions;
	}

}
